package com.kim.bookshop.user.action;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import com.kim.bookshop.cart.bean.Cart;
import com.kim.bookshop.user.bean.User;
import com.kim.bookshop.user.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.itcast.commons.CommonUtils;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import exception.BookShopException;

public class UserAction extends ActionSupport {

	
	private User form;
	private static final long serialVersionUID = -398151888549579232L;

	
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	private ApplicationContext ctx;	
	private UserService userService;
	
	public UserAction(){
		request=ServletActionContext.getRequest();
		session=request.getSession();
		application= session.getServletContext();
		ctx=(ApplicationContext) application.getAttribute("ApplicationContext"); 
		userService=(UserService) ctx.getBean("userService");
		
	}
	
	/**
	 * 用户注册
	 * @return
	 * @throws IOException 
	 */
	public  String regist() throws IOException {
	
		//补全信息
		form.setUid(CommonUtils.uuid());
		form.setCode(CommonUtils.uuid()+CommonUtils.uuid());
		
		try {
			System.out.println(form);
			userService.register(form);
			//发送激活邮件
			Properties props = new Properties();
			props.load(this.getClass().getClassLoader()
					.getResourceAsStream("email_template.properties"));
			String host = props.getProperty("host");//获取服务器主机
			String uname = props.getProperty("uname");//获取用户名
			String pwd = props.getProperty("pwd");//获取密码
			String from = props.getProperty("from");//获取发件人
			String to = form.getEmail();//获取收件人
			String subject = props.getProperty("subject");//获取主题
			String content = props.getProperty("content");//获取邮件内容
			content = MessageFormat.format(content, form.getCode());//替换{0}
			
			Session session = MailUtils.createSession(host, uname, pwd);//得到session
			Mail mail = new Mail(from, to, subject, content);//创建邮件对象
			try {
				MailUtils.send(session, mail);//发邮件！
			//	request.setAttribute("msg", "注册成功！请马上到邮箱激活");
				return "active";
			} catch (MessagingException e) {
				return "active";
			}
			
		} catch (BookShopException e) {
			request.setAttribute("olduser", form);
			request.setAttribute("msg", e.getMessage());
			return "fail";
		}
		
	}
	
	/**
	 * 管理员登录	
	  */
	@Override
	public String execute() {
		try {
			System.out.println(form.getUserName());
			System.out.println(form.getPassword());
			userService.admin(form);
			session.setAttribute("admin", form);
		} catch (BookShopException e) {
			System.out.println(e.getMessage());
			request.setAttribute("msg", e.getMessage());
			return "fail";
		}
		return SUCCESS;
	}

	/**
	 * 用户登录
	 * @return
	 */
	public String login(){
		try {
			System.out.println("用户登录");
			System.out.println(form.getUserName());
			System.out.println(form.getPassword());
			
			User user=userService.login(form);
			session.setAttribute("user", user);
			request.getSession().setAttribute("cart", new Cart());
			return SUCCESS;
		} catch (BookShopException e) {
			request.setAttribute("msg", e.getMessage());
			return "fail";
		}
	}
	
	/**
	 * 用户退出
	 * @return
	 */
	public String quit(){
			request.getSession().invalidate();
		return "quit";
	}
	
	//管理员退出
	public String quitadmin(){
		request.getSession().invalidate();
		return "quitadmin";
	}
	
	/**
	 * 激活
	 * @return
	 */
	public String active() {
		//获取参数激活码
		String code=request.getParameter("code");
		try {
			userService.active(code);
			//request.setAttribute("msg", "激活成功，马上登录");
			return "active";
		} catch (BookShopException e) {
			request.setAttribute("msg", e.getMessage());
			return "active";
		}
	}
	
	/**
	 * 管理员列表
	 * @return
	 */
	public String getadmin(){
		System.out.println("test");
		request.setAttribute("adminList", userService.getAdmin());
		return "adminlist";
	}
	
	/**
	 * 用户列表
	 * @return
	 */
	public String getlist(){
		request.setAttribute("userList", userService.getList());
		return "list";
	}
	
	/**
	 * 加载
	 * @return
	 */
	public String loadmsg(){
		String uid=request.getParameter("uid");
		request.setAttribute("user",userService.load(uid));
		return "desc";
	}
	
	/**
	 * 禁用
	 * @return
	 */
	public String kill(){
		String  uid=request.getParameter("uid");
		System.out.println(uid);
		userService.kill(uid);
		return "listuser";
	}
	
	/**
	 * 启用
	 * @return
	 */
	public String  restart(){
			userService.restart(request.getParameter("uid"));
		return "listuser";
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String updatemsg(){
		System.out.println(form.getUid());
		System.out.println(form.getUserName());
		userService.updatemsg(form);
		return "listuser";
	}

	public String delete(){
		String uid=request.getParameter("uid");
		System.out.println(uid);
		userService.remove(uid);
		return "listuser";
	} 
	
	public User getForm() {
		return form;
	}

	public void setForm(User form) {
		this.form = form;
	}

	


	
	
	
	
}
